package com.aptemo.webetek.server.data;

import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.aptemo.webetek.client.enums.EnumBetStatus;
import com.aptemo.webetek.client.enums.EnumBetType;
import com.aptemo.webetek.shared.BetBean;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * Stake is a User child class (owned 1:N bidirectional relationship)
 * 
 * Contains picks (unowned 1:N relationship)
 * 
 * @author coh
 * 
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Stake
{

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key				id;

	@SuppressWarnings("unused")
	@Persistent
	@Extension(vendorName = "datanucleus", key = "gae.parent-pk", value = "true")
	private Key				parentKey;

	private String			sid;

	// unowned relationship (picks might be child of other classes (bets))
	@Persistent
	private List<Key>		pickKeys;

	// owned 1:N bidirectional relationship
	// @Persistent
	// private WebetekUser player;

	@Persistent
	private Double			amount;

	@Persistent
	private EnumBetStatus	status	= EnumBetStatus.OPENED;

	@Persistent
	private Date			date;

	@Persistent
	private EnumBetType		betType	= EnumBetType.SINGLE;

	@Persistent
	private Double			earnings;

	/**
	 * The cumulative value of odds valid on exact placing bet date.
	 */
	@Persistent
	private Double			totalOdds;

	/*
	 * Number of wins (for efficient statistics)
	 */
	@Persistent
	private Integer			wonCnt;

	public Stake(double amount, List<Key> pickKeys, EnumBetType bettype)
	{
		setPickKeys(pickKeys);
		setAmount(amount);
		setBetType(bettype);

		this.date = new Date();
	}

	public Key getId()
	{
		return id;
	}

	public void setAmount(double amount)
	{
		this.amount = amount;
	}

	public double getAmount()
	{
		return amount;
	}

	public void setStatus(EnumBetStatus status)
	{
		this.status = status;
	}

	public EnumBetStatus getStatus()
	{
		return status;
	}

	public String getSid()
	{
		if (sid == null)
		{
			this.sid = KeyFactory.keyToString(id);
		}
		return sid;
	}

	public Date getDate()
	{
		return date;
	}

	public void setPickKeys(List<Key> pickKeys)
	{
		this.pickKeys = pickKeys;
	}

	public List<Key> getPickKeys()
	{
		return pickKeys;
	}

	public BetBean toBean()
	{

		BetBean stake = new BetBean(amount, date);

		// TODO performace overkill
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try
		{
			for (Key key : pickKeys)
			{
				Pick pick = pm.getObjectById(Pick.class, key);
				stake.addPick(pick.toBean(pick.getParent(pm).toBean()));
			}
		}
		finally
		{
			pm.close();
		}
		stake.setId(getSid());
		stake.setBetType(betType);
		stake.setStatus(status);
		stake.setEarnings(earnings);
		stake.setTotalOdds(getTotalOdds());

		return stake;
	}

	public void setBetType(EnumBetType betType)
	{
		this.betType = betType;
	}

	public EnumBetType getBetType()
	{
		return betType;
	}

	public void setEarnings(Double won)
	{
		this.earnings = won;
	}

	public Double getEarnings()
	{
		if (earnings != null)
		{
			return earnings;
		}
		else
		{
			return 0.0;
		}
	}

	public int getWonCnt()
	{
		return wonCnt;
	}

	public void setWonCnt(int wonCnt)
	{
		this.wonCnt = wonCnt;
	}

	public Double getTotalOdds()
	{
		if (totalOdds == null)
		{
			return 0.0;
		}
		return totalOdds;
	}

	public void setTotalOdds(double totalOdds)
	{
		this.totalOdds = totalOdds;
	}

}
